// 对象深拷贝

export function isPlainObject(val: any): val is Object {
    return toString.call(val) === '[object Object]'
}
export function deepMerge(...source: any[]): any {
    if (null == source || [] == source) return source;
    if (typeof source !== "object") return source;
    if (source instanceof RegExp) return new RegExp(source)
    const result = Object.create(null)
    source.forEach(obj => {
        if (obj) {
            Object.keys(obj).forEach(key => {
                const val = obj[key]
                if (isPlainObject(val)) {
                    // 递归
                    if (isPlainObject(result[key])) {
                        result[key] = deepMerge(result[key], val)
                    } else {
                        result[key] = deepMerge(val)
                    }
                } else {
                    result[key] = val
                }
            })
        }
    })
    return result
}

// 对象的深拷贝
export class objectDeepClone {

    // 不做任何处理
    static use(...source: any): any {
        if (null == source || [] == source) return source;
        if (typeof source !== "object") return source;
        if (source instanceof RegExp) return new RegExp(source)
        const result = Object.create(null)
        source.forEach((obj: any) => {
            if (obj) {
                Object.keys(obj).forEach(key => {
                    const val = obj[key]
                    if (isPlainObject(val)) {
                        // 递归
                        if (isPlainObject(result[key])) {
                            result[key] = objectDeepClone.use(result[key], val)
                        } else {
                            result[key] = objectDeepClone.use(val)
                        }
                    } else {
                        result[key] = val
                    }
                })
            }
        })
        return result
    }
    // 可以指定层数
    static select(number: number, ...source: any): any {
        if (null == source || [] == source) return source;
        if (typeof source !== "object") return source;
        if (source instanceof RegExp) return new RegExp(source)
        const result = Object.create(null)
        source.forEach((obj: any) => {
            if (obj) {
                Object.keys(obj).forEach(key => {
                    const val = obj[key]
                    if (isPlainObject(val) && number > 0) {
                        number--;
                        // 递归
                        if (isPlainObject(result[key])) {
                            result[key] = objectDeepClone.select(number, result[key], val)
                        } else {
                            result[key] = objectDeepClone.select(number, val);
                        }
                    } else {
                        result[key] = val
                    }
                })
            }
        })
        return result
    }
}





// 数组深拷贝
export class arrayDeepClone {
    // 不做任何处理的深拷贝
    static use(source: any) {
        if (!source) return null
        if (typeof source != "object") source
        let target: any;

    }
    // 拷贝指定层数
    static select(source: any, number: number) {
        if (!source) return null
        if (typeof source != "object") source
    }
}
